<template>
  <div class="rank">
    <Header />
    <div class="list" v-for="item in topList " :key="item.title">
      <h3 class="list-title">{{"「"+item.title+"」"}}</h3>
      <div
        class="rank-list"
        v-for="itemList in item.list"
        :key="itemList.topId"
        @click="selectRankList(itemList)"
      >
        <div class="item item-w">
          <h4>{{itemList.label}}</h4>
          <ul>
            <li v-for="(musicItem,i) in itemList.song" :key="itemList.topId+musicItem.songId">
              <span class="item-w-no">{{i+1}}.</span>
              <span
                class="item-w-songname"
              >{{cutstr(musicItem.title + " - " + musicItem.singerName, wordsLimit())}}</span>
              <!-- <span class="item-w-artist">{{cutstr(musicItem.singerName, 6)}}</span> -->
            </li>
          </ul>
        </div>
        <div class="item item-bd">
          <div class="item-img">
            <img :src="itemList.picUrl" />
          </div>
          <div class="item-playnum">
            <img src="">
            <span>{{toFixed((itemList.listenNum/10000),1)}}万</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import axios from "axios";
export default {
  name: "Rank",
  components: {
  },
  data() {
    return {
      topList: []
    };
  },
  created() {
    this.getTopList();
  },
  methods: {
    getTopList() {
      axios
        .get("/jsososo/top/category", {
          params: {
            showDetail: 1
          }
        })
        .then(res => {
          // console.log(res.data.data);
          this.topList = res.data.data;
        })
        .catch(err => {
          console.error(err);
        });
    },
    toFixed(number, fractionDigits) {
      //number 传入数字
      //fractionDigits 保留小数位数
      var times = Math.pow(10, fractionDigits);
      var roundNum = Math.round(number * times) / times;
      return roundNum.toFixed(fractionDigits);
    },
    // 截取字符串
    cutstr(str, len) {
      var str_length = 0;
      var str_cut = new String();
      var str_len = str.length;
      for (var i = 0; i < str_len; i++) {
        var a = str.charAt(i);
        str_length++;
        if (escape(a).length > 4) {
          //中文字符的长度经编码之后大于4
          str_length++;
        }
        str_cut = str_cut.concat(a);
        if (str_length >= len) {
          str_cut = str_cut.concat("...");
          return str_cut;
        }
      }
      //如果给定字符串小于指定长度，则返回源字符串；
      if (str_length < len) {
        return str;
      }
    },
    wordsLimit() {
      var width = document.body.clientWidth - 30 - 120 - 15 - 10;
      return width / 8;
    },
    selectRankList(item){
      const id = item.topId;
      this.$router.push({
        name: 'RankListDetail',
        params: {id},
      })
    }
  }
};
</script>
<style scoped>
.rank {
  padding-bottom: 50px;
}
.list-title {
  color: #1a1a1a;
}
.list {
  background-color: #f7f7f7;
  padding-bottom: 0.2rem;
  padding-top: 75px;
}
.list h3 {
  margin: 0;
}

.rank-list {
  display: flex;
  margin: 0.8em 15px;
  background-color: #fff;
  border-radius: 8px;
}
.item-w {
  width: calc(100% - 120px);
  padding: 15px 0 0 15px;
}
.item-w-no {
  font-weight: bold;
  color: #1a1a1a;
}
.item-w-artist {
  color: rgba(26, 26, 26, 0.5);
}
.item-bd {
  width: 120px;
  height: 120px;
  border-radius: 0 8px 8px 0;
}
.item-img img {
  width: 120px;
  height: 120px;
  float: right;
  border-radius: 0 8px 8px 0;
}
.item-playnum {
  height: 20px;
  line-height: 20px;
  position: relative;
  bottom: 20px;
  font-size: 12px;
  float: right;
  color: #fff;
  background: rgba(0, 0, 0, 0.4);
  padding: 0 10px;
  border-radius: 10px;
}
.item-playnum img{
  height: 10px;
  vertical-align: baseline;
  line-height: 20px;
}
.item-playnum span{
  padding-left: 2px;
}
.item h4 {
  margin: 0;
  font-size: 18px;
  color: #000000;
  text-align: left;
}
.item li {
  text-align: left;
  padding: 2px 0;
}
.item li span {
  font-size: 13px;
}
.item > img {
  width: 120px;
}
</style>
